package com.maple.controller.admin;


import com.maple.result.Result;
import com.maple.service.ReportService;
import com.maple.vo.OrderReportVO;
import com.maple.vo.SalesTop10ReportVO;
import com.maple.vo.TurnoverReportVO;
import com.maple.vo.UserReportVO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/report")
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;


    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> ordersStatistics(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                  @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("订单统计接口：{}{}", begin, end);
        OrderReportVO orderReportVO = reportService.ordersStatistics(begin, end);
        return Result.success(orderReportVO);
    }


    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> getTop10(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                               @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("查询销量排名top10接口：{}{}", begin, end);
        SalesTop10ReportVO salesTop10ReportVO = reportService.getTop10(begin, end);
        return Result.success(salesTop10ReportVO);
    }


    @GetMapping("/userStatistics")
    public Result<UserReportVO> getUserStatistics(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                  @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("用户统计接口：{}{}", begin, end);
        UserReportVO userReportVO = reportService.getUserStatistics(begin, end);
        return Result.success(userReportVO);
    }


    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> getTurnover(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("营业额统计接口：{}{}", begin, end);
        TurnoverReportVO turnoverReportVO = reportService.getTurnover(begin, end);
        return Result.success(turnoverReportVO);
    }


    @GetMapping("/export")
    public Result exportExcel(HttpServletResponse response){
        log.info("导出Excel报表接口{}", LocalDateTime.now());
        reportService.exportBusinessData(response);
        return Result.success();
    }



}
